package org.setamv.springcloud;

import okhttp3.OkHttpClient;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableEurekaClient
public class ServiceApplication
{
    public static final String DEFAULT_APP_PORT = "9091";

    public static void main( String[] args )
    {
//        Scanner scanner = new Scanner(System.in);
//        System.out.println("请输入应用启动的端口号。");
//        System.out.println("如果已经在配置文件application.properties(.yml)中设置了端口号，将忽略这里的设置。如果不输入端口号直接回车，将使用默认的端口号9091");
//        System.out.print("应用启动的端口号：");
//        String port = scanner.nextLine();
//        if (StringUtils.isBlank(port)) {
//            port = DEFAULT_APP_PORT;
//        }
        new SpringApplicationBuilder(ServiceApplication.class)
//                .properties("server.port=" + port)
                .web(true).run(args);
    }

    @Bean
    public HttpClient getHttpClient() {
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
        connectionManager.setMaxTotal(500);
        connectionManager.setDefaultMaxPerRoute(200); //例如默认每路由最高50并发，具体依据业务来定

        HttpClient httpClient = HttpClients.custom()
                .setConnectionManager(connectionManager)
                .setDefaultRequestConfig(RequestConfig.custom().setStaleConnectionCheckEnabled(true).build())
                .build();

        return httpClient;
    }

    @Bean
    public OkHttpClient getOkHttpClient() {
        OkHttpClient httpClient = new OkHttpClient();
        httpClient.dispatcher().setMaxRequests(500);
        httpClient.dispatcher().setMaxRequestsPerHost(200);
        return httpClient;
    }
}
